package ru.papergames.web.blines.battle.network.serverpackets;

import ru.papergames.common.form.PcPlayerForm;
import ru.papergames.web.cache.PlayersManager;

/**
 * @author: atroshanin
 * Date: 19.09.2010
 * Time: 17:48:45
 */
public class FlashUserDisconnected extends WebServerServerPacket {

    private int playerId;

    @Override
    protected void readImpl() {
        playerId = readD();
    }

    @Override
    protected void runImpl() {
        getClient().getConnectionHolder().decUsersCount();
        
        PcPlayerForm player = PlayersManager.getInstance().getActivePlayer(playerId);
        if (player == null) {
            return;
        }
        player.setDisconnectedToBattleServer();
        player.executeInLock(new PcPlayerForm.ExecutorInLock<Void>() {
            @Override
            public Void execute() {
                PlayersManager.getInstance().removePlayerFormFighterPools(playerId);
                return null;
            }
        });
    }

    @Override
    public String getType() {
        return "[s] FlashUserDisconnected [" + FLASH_USER_DISCONNECTED_PACKET_ID + "]";
    }
}
